package com.dingup.provider.controller.log;

import com.dingup.api.Util.DateUtil;
import com.dingup.api.base.HttpResContent;
import com.dingup.api.enums.log.LogUpdateTypeEnum;
import com.dingup.api.model.sys.SysLog;
import com.dingup.provider.service.log.SysLogService;
import com.suyeer.basic.util.BHttpResUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

import static com.dingup.api.base.BaseUtil.saveErrorMsgAndData;
import static com.dingup.api.base.BaseUtil.saveSuccessMsgAndData;

/**
 * Created by wancheng on 2018/8/29.
 */
@RestController()
@RequestMapping("/log")
public class SysLogController {

    @Autowired
    SysLogService sysLogService;

    @RequestMapping(value = "/get_class_info_log_by_id.do",method = RequestMethod.POST)
    public void getLogById(HttpServletRequest request, HttpServletResponse response,
                             Integer id) {
        HttpResContent<List<SysLog>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取日志");
        try {
            LogUpdateTypeEnum[] type = {LogUpdateTypeEnum.ADD,LogUpdateTypeEnum.UPDATE,LogUpdateTypeEnum.UPDATE_ADD,LogUpdateTypeEnum.UPDATE_DELETE,LogUpdateTypeEnum.UPDATE_STATUS};
            Integer[] modelId = {id};
            List<SysLog> logs = sysLogService.getClassLog((List<LogUpdateTypeEnum>) Arrays.asList(type),Arrays.asList(modelId));
            logs.stream().forEach(
                    l->{
                        l.setCreateTimeString(DateUtil.date2Str(l.getCreateTime(), "yyyy-MM-dd HH:mm"));
                    }
            );
            saveSuccessMsgAndData(hrc, logs);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_class_info_log.do",method = RequestMethod.POST)
    public void getLog(HttpServletRequest request, HttpServletResponse response
                       ) {
        HttpResContent<List<SysLog>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取班级日志");
        try {
            LogUpdateTypeEnum[] type = {LogUpdateTypeEnum.DELETE,LogUpdateTypeEnum.ADD,LogUpdateTypeEnum.UPDATE_STATUS,LogUpdateTypeEnum.UPDATE_CLASS_END};
            List<SysLog> logs = sysLogService.getClassLog((List<LogUpdateTypeEnum>) Arrays.asList(type),null);
            logs.stream().forEach(
                    l->{
                        l.setCreateTimeString(DateUtil.date2Str(l.getCreateTime(),"yyyy-MM-dd hh:mm"));
                    }
            );
            saveSuccessMsgAndData(hrc, logs);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson_info_log_by_id.do",method = RequestMethod.POST)
    public void getLessonLogById(HttpServletRequest request, HttpServletResponse response,
                           Integer id) {
        HttpResContent<List<SysLog>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取日志");
        try {
            LogUpdateTypeEnum[] type = {LogUpdateTypeEnum.ADD,LogUpdateTypeEnum.UPDATE,LogUpdateTypeEnum.UPDATE_ADD,LogUpdateTypeEnum.UPDATE_DELETE};
            Integer[] modelId = {id};

            List<SysLog> logs = sysLogService.getLessonLog((List<LogUpdateTypeEnum>) Arrays.asList(type),Arrays.asList(modelId));
            logs.stream().forEach(
                    l->{
                        l.setCreateTimeString(DateUtil.date2Str(l.getCreateTime(),"yyyy-MM-dd hh:mm"));
                    }
            );
            saveSuccessMsgAndData(hrc, logs);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson_info_log.do",method = RequestMethod.POST)
    public void getLessonLog(HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<List<SysLog>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取课程上下架日志");
        try {
            LogUpdateTypeEnum[] type = {LogUpdateTypeEnum.ADD,LogUpdateTypeEnum.UPDATE_LESSON_LOWER_FRAME,LogUpdateTypeEnum.UPDATE_LESSON_ON_SHELF};
            List<SysLog> logs = sysLogService.getLessonLog((List<LogUpdateTypeEnum>) Arrays.asList(type),null);
            logs.stream().forEach(
                    l->{
                        l.setCreateTimeString(DateUtil.date2Str(l.getCreateTime(),"yyyy-MM-dd hh:mm"));
                    }
            );
            saveSuccessMsgAndData(hrc, logs);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }
}
